/*
 * @Description: 你的代码我的心
 * @Author: GGB
 * @Date: 2022-01-04 18:03:24
 * @LastEditors: GGB
 * @LastEditTime: 2022-09-14 15:27:52
 */
import { RYEventEnum } from '@/enums/RYEventEnum'
import * as RongIMLib from '@rongcloud/imlib-next'
import { notification } from 'ant-design-vue'
import mitt from '/@/utils/mitt'
// 订单消息通知
export const RYEmitter = mitt()
const environment = process.env.NODE_ENV !== 'production'
if (environment) {
  RongIMLib.init({ appkey: 'z3v5yqkbzi0g0' })
} else {
  RongIMLib.init({ appkey: 'z3v5yqkbzi0g0' })
}

// 定义融云的事件
const Events = RongIMLib.Events
/**
 * 正在链接的事件状态
 */
RongIMLib.addEventListener(Events.CONNECTING, () => {
  console.log('正在链接...')
})

/**
 * 链接到服务器会触发这个事件
 */
RongIMLib.addEventListener(Events.CONNECTED, () => {
  console.log('连接成功')
})

/**
 * 手动调用 disconnect 方法或者用户被踢下线 会触发这个事件
 */
RongIMLib.addEventListener(Events.DISCONNECT, async () => {
  console.log('连接中断，需要业务层进行重连处理')
})

/**
 * 链接出问题时，内部进行重新链接，会出发这个事件
 */
RongIMLib.addEventListener(Events.SUSPEND, () => {
  console.log('链接中断，SDK 会尝试重连，业务层无需关心')
})
/**
 * 获取消息
 */
RongIMLib.addEventListener(Events.MESSAGES, (res) => {
  const messages: any = res.messages
  const now = Date.parse(new Date().toString()) / 1000
  if (messages?.length) {
    messages.forEach((message) => {
      let type = message.messageType
      let sendTimestamp = message.content.data ? message.content.data.timestamp : message.content.timestamp
      let content = message.content.data ? message.content.data.content : message.content.notice
      if (now - sendTimestamp <= 600) {
        messageTypeList.forEach((messageType) => {
          if (messageType.type === type && messageType.audio) {
            // 播放音频 方式一
            let audio = new Audio()
            audio.src = messageType.audio
            audio.play()

            // // 方式二
            // let audio = document.createElement('audio');
            // // require一个本地文件，会变成base64格式
            // audio.src = messageType.audio;
            // document.body.appendChild(audio);
            // // play返回的是一个promise
            // audio.play()
          }
        })
        notification.success({
          message: '温馨提示',
          description: content,
          duration: 10,
        })
        // 这里后期需要对数据进行特殊处理
        RYEmitter.emit(RYEventEnum.NEW_ORDER, true)
      }
    })
  }
})

export async function RongIMLibInit(token) {
  if (!token) return
  const linkInfo = await RongIMLib.connect(token)
  console.log(linkInfo)
  if (linkInfo.code !== 0) {
    return notification.warning({
      message: '温馨提示',
      description: '链接错误',
      duration: 10,
    })
  }
}
// 获取融云的消息
const messageTypeList = [
  {
    id: 1,
    message: '你有一个新的外卖订单,请处理',
    type: 'APP:NOTICENEWORDER',
    title: '外卖订单',
    description: '新订单',
    audio: 'https://delivery.yrdspbd.com/images/10001.mp3',
  },
  {
    id: 2,
    message: '有订单申请取消，请处理',
    type: 'APP:NOTICECANCELORDER',
    title: '外卖订单',
    description: '新订单',
  },
  {
    id: 3,
    message: '有订单申请取消，请处理',
    type: 'LAYIM:BOX',
    title: '外卖订单',
    description: ' 有订单申请取消，请处理',
    auido: 'https://delivery.yrdspbd.com/images/10005.mp3',
  },
  {
    id: 4,
    message: '有订单申请取消，请处理',
    type: 'APP:NOTICEAFTERSALEORDER',
    title: '外卖订单',
    description: '有订单申请售后，请处理！',
  },
]

/**
 * 关闭连接
 */
export function RongIMLibClose() {
  RongIMLib.disconnect()
}
